﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ISHotelApp.Database;

namespace ISHotel_WinForm
{
    public partial class PobytyForm : Form
    {
        public PobytyForm()
        {
            InitializeComponent();

            TaxaTable taxaTable = new TaxaTable();
            TaxaCB.DataSource = taxaTable.Select();
            TaxaCB.DisplayMember = "nazev_taxy";
            TaxaCB.ValueMember = "id_taxy";
            HostTable hostTable = new HostTable();
            HostCB.DataSource = hostTable.Select();
            HostCB.DisplayMember = "prijmeni";
            HostCB.ValueMember = "rodne_cislo";
            PokojTable pokojTable = new PokojTable();
            PokojCB.DataSource = pokojTable.Select();
            PokojCB.DisplayMember = "id_pokoje";
            PokojCB.ValueMember = "id_pokoje";
            StravovaniTable stravovaniTable = new StravovaniTable();
            StravovaniCB.DataSource = stravovaniTable.Select();
            StravovaniCB.DisplayMember = "nazev_stravovani";
            StravovaniCB.ValueMember = "id_stravovani";

            PobytTable pobytTable = new PobytTable();
            dataGridViewPobyty.AutoGenerateColumns = false;
            
            DataGridViewTextBoxColumn idpobytuColumn = new DataGridViewTextBoxColumn();
            idpobytuColumn.DataPropertyName = "id_pobytu";
            idpobytuColumn.HeaderText = "ID Pobytu";

            DataGridViewTextBoxColumn idtaxyColumn = new DataGridViewTextBoxColumn();
            idtaxyColumn.DataPropertyName = "id_taxy";
            idtaxyColumn.HeaderText = "ID Taxy";

            DataGridViewTextBoxColumn prijezdColumn = new DataGridViewTextBoxColumn();
            prijezdColumn.DataPropertyName = "prijezd";
            prijezdColumn.HeaderText = "Prijezd";

            DataGridViewTextBoxColumn odjezdColumn = new DataGridViewTextBoxColumn();
            odjezdColumn.DataPropertyName = "odjezd";
            odjezdColumn.HeaderText = "Odjezd";

            DataGridViewTextBoxColumn celkovaCenaColumn = new DataGridViewTextBoxColumn();
            celkovaCenaColumn.DataPropertyName = "celkova_suma";
            celkovaCenaColumn.HeaderText = "Celkova cena";

            DataGridViewTextBoxColumn zaplacenoColumn = new DataGridViewTextBoxColumn();
            zaplacenoColumn.DataPropertyName = "zaplaceno";
            zaplacenoColumn.HeaderText = "Zaplaceno";

            dataGridViewPobyty.DataSource = pobytTable.Select();

            dataGridViewPobyty.Columns.Add(idpobytuColumn);
            dataGridViewPobyty.Columns.Add(idtaxyColumn);
            dataGridViewPobyty.Columns.Add(prijezdColumn);
            dataGridViewPobyty.Columns.Add(odjezdColumn);
            dataGridViewPobyty.Columns.Add(celkovaCenaColumn);
            dataGridViewPobyty.Columns.Add(zaplacenoColumn);
        }

        private void dataGridViewPobyty_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            ErrLabel.Text = "";
            if (e.RowIndex >= 0)
            {
                button_update.Enabled = true;
                button_delete.Enabled = true;
                int id_pobytu = (int)dataGridViewPobyty.Rows[e.RowIndex].Cells[0].Value;
                IdPobytuTB.Text = id_pobytu.ToString();
                PobytTable pobytTable = new PobytTable();
                Pobyt pobyt = pobytTable.Select(id_pobytu);
                PrijezdDP.Value = pobyt.prijezd;
                OdjezdDP.Value = pobyt.odjezd;
                TaxaCB.SelectedValue = pobyt.id_taxy;
                SeznamHostuTable seznamHostuTable =new SeznamHostuTable();
                SeznamHostu seznamHostu = seznamHostuTable.Select(pobyt.id_pobytu);
                HostCB.SelectedValue = seznamHostu.rodne_cislo;
                ObjednavkaPokojeTable objednavkaPokojeTable = new ObjednavkaPokojeTable();
                ObjednavkaPokoje objednavkaPokoje = objednavkaPokojeTable.Select(pobyt.id_pobytu);
                PokojCB.SelectedValue = objednavkaPokoje.id_pokoje;
                ObjednavkaStravovaniTable objednavkaStravovaniTable = new ObjednavkaStravovaniTable();
                ObjednavkaStravovani objednavkaStravovani = objednavkaStravovaniTable.Select(pobyt.id_pobytu);
                StravovaniCB.SelectedValue = objednavkaStravovani.id_stravovani;
                if (pobyt.zaplaceno == 1) ZaplacenoChB.Checked = true;
                else ZaplacenoChB.Checked = false;
            }
        }

        private void button_insert_Click(object sender, EventArgs e)
        {
            ErrLabel.Text = "";
            if (PrijezdDP.Value < OdjezdDP.Value)
            {
                Pobyt newPobyt = new Pobyt();
                newPobyt.id_taxy = (int)TaxaCB.SelectedValue;
                newPobyt.prijezd = PrijezdDP.Value;
                newPobyt.odjezd = OdjezdDP.Value;
                newPobyt.id_pokoje = (int)PokojCB.SelectedValue;
                newPobyt.rodne_cislo = HostCB.SelectedValue as string;
                newPobyt.id_stravovani = (int)StravovaniCB.SelectedValue;

                PobytTable addSourceRecord = new PobytTable();

                try
                {
                    addSourceRecord.Insert(newPobyt);
                    dataGridViewPobyty.DataSource = addSourceRecord.Select();
                }
                catch (Exception ex)
                {

                    if (ex.Message.ToString().IndexOf("ORA-01422") >= 0) { ErrLabel.Text = "Pokoj je v daném datu již obsazen"; }
                    else { ErrLabel.Text = ex.Message.ToString(); };
                }
            }
            else ErrLabel.Text = "Datum prijezdu musi byt drive nez datum odjezdu.";
        }

        private void button_update_Click(object sender, EventArgs e)
        {
            ErrLabel.Text = "";
            Pobyt newPobyt = new Pobyt();
            PobytTable pobytTable = new PobytTable();
            newPobyt.id_pobytu = Convert.ToInt32(IdPobytuTB.Text);
            newPobyt.id_taxy = (int)TaxaCB.SelectedValue;
            newPobyt.prijezd = PrijezdDP.Value;
            newPobyt.odjezd = OdjezdDP.Value;
            newPobyt.id_pokoje = (int)PokojCB.SelectedValue;
            newPobyt.rodne_cislo = HostCB.SelectedValue as string;
            newPobyt.id_stravovani = (int)StravovaniCB.SelectedValue;
            pobytTable.Update(newPobyt);
            if (ZaplacenoChB.Checked)
            {
                
                pobytTable.UpdateZaplaceno(newPobyt.id_pobytu, 1);
            }
            else
            {
                pobytTable.UpdateZaplaceno(newPobyt.id_pobytu, 0);
            }
            dataGridViewPobyty.DataSource = pobytTable.Select();
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            ErrLabel.Text = "";
            PobytTable delSourceRecord = new PobytTable();
            delSourceRecord.Delete(Convert.ToInt32(IdPobytuTB.Text));
            PobytTable pobytTable = new PobytTable();
            dataGridViewPobyty.DataSource = pobytTable.Select();
        }

        private void hosteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HosteForm hosteForm = new HosteForm();
            hosteForm.Show();
        }

        private void sluzbyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SluzbyForm sluzbyForm = new SluzbyForm();
            sluzbyForm.Show();
        }

        private void pokojeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PokojeForm pokojeForm = new PokojeForm();
            pokojeForm.Show();
        }

        private void stravovaniToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StravovaniForm stravovaniForm = new StravovaniForm();
            stravovaniForm.Show();
        }

        private void hosteXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HosteXML hosteXml = new HosteXML();
            hosteXml.Show();
        }
    }
}
